define(['butterfly/view'], function(View) {

  //show only contain one direct subview
  return View.extend({

    initialize: function(options) {
      View.prototype.initialize.apply(this, arguments);

      // stack to store the views, [{path: 'a', view: a}, {path: 'b', view: b}]
      this.stack = [];
      this.baseZIndex = 100;

      // using this variable to indicate this container has routed once already
      // to stop the animation from first route
      // this check is no needed for normal case
      this.routedOnce = false;
    },

    render: function() {
      _.each(this.stack, function(item) {
        item.view.render();
      });
    },

    //pass on the 'onShow' event to the top subview
    onShow: function(options) {
      var currentView = this.stack[this.stack.length - 1].view;
      currentView.show(options);
    },

    addSubview: function(view) {
      View.prototype.addSubview.apply(this, arguments);
      view.$el.css({
        'position': 'absolute',
        'top': '0px',
        'bottom': '0px',
        'width': '100%',
        'z-index': this.baseZIndex++
      });

      this.stack.push({
        path: "",
        view: this.subviews[0]
      });
    },

    route: function(paths, options) {

      // console.log(paths);
      var me = this;

      if (this.stack.length == 1 && !paths) {
        this.routedOnce = true;
        return;
      }

      // check is this route is intent to go back
      // console.log(this.stack);

      var goingBack = this.stack.length >= 2 && (this.stack[this.stack.length - 2].path == paths);
      // 2 top views in the stack
      var currentView = this.stack[this.stack.length - 1].view;
      var nextView = this.stack.length >= 2 ? this.stack[this.stack.length - 2].view : null;


      if (goingBack) {
        this.stack.pop();
        this.baseZIndex--;
        // nextView.animateSlideInLeft();
        // currentView.animateSlideOutRight(function(){
        //hide & remote top
        $(nextView.el).show();
        currentView.hide();
        currentView.remove();
        //show next

        nextView.show(options);
        // });
      } else {
        var delete_flag = 0;
        var count_d = 0;
        var stack_length = me.stack.length;

        for (var i = 0; i < stack_length; i++) {
          if (me.stack[i].path == paths) {
            delete_flag = 1;
            count_d = i + 1;
          }
        }

        if (delete_flag == 1) {
          for (var i = stack_length - 1; i >= count_d; i--) {
            // console.log(me.stack[i].view);
            me.stack[i].view.remove();
            me.stack.pop();
            me.baseZIndex--;
          }
             // console.log(me.stack[count_d-1].view);
             $(me.stack[count_d-1].view.el).show();
              if (window.location.href.indexOf("#com.52zzb.main/orderonegogo") > 0) {
                me.stack[count_d-1].view.show(options);

              }





        } else {

          //load view using butterfly plugin
          require(['view!' + paths], function(ViewClass) {
            var newView = new ViewClass();
            newView.$el.css({
              'position': 'absolute',
              'top': '0px',
              'bottom': '0px',
              'width': '100%',
              'z-index': me.baseZIndex++
            });
              me.stack.push({
              path:paths,
              view: newView
            });
            if (currentView) {
              currentView.hide();
              // console.log(currentView);
              $(currentView.el).hide();
              // currentView.remove();
            }
            newView.render();
            me.el.appendChild(newView.el);
            newView.show(options);
            //如果是第一次route，则不显示动画
            if (me.routedOnce) {
              // currentView.animateSlideOutLeft();
              // newView.animateSlideInRight();
              // var animations = ['slideInLeft', 'slideInRight', 'slideOutLeft', 'slideOutRight', 'slideInUp', 'slideInDown', 'slideOutUp', 'slideOutDown'];
              // newView.animateSlideInDown();
              // currentView.animateSlideOutUp();
            }
          
            me.routedOnce = true;
          }, function(err) {
            //TODO: without trigger
            window.history.back();
            alert('页面加载失败');
          });



        }


      }


    }
  });
});